package com.tuanzi.service.impl;

import com.tuanzi.ResultVo.ResultVO;
import com.tuanzi.dao.UserMapper;
import com.tuanzi.entity.User;
import com.tuanzi.service.LoginService;
import com.tuanzi.utils.Status;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author tuanzi
 * @version 1.0
 * @date 2022/3/30 13:39
 */

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private UserMapper userMapper;

    /**
     *
     * @param username 用户名
     * @param password 密码
     * @return RsultVo类，包含了是否登录成功的状态码，还有返回的对象
     *
     * 在这里也添加一下事务
     */
    @Transactional
    @Override
    public ResultVO login(String username, String password) {
        ResultVO vo = new ResultVO(null);
        User user = userMapper.selectByUserName(username);
        if(user == null){
            vo.setStatus(Status.NOACCOUNT);
        }else {
            if (user.getPassword().equals(password)){
                vo.setStatus(Status.LOGINSUCCESS);
                vo.setContent(user);
            }else {
                vo.setStatus(Status.PASSWORDFAIL);
            }
        }
        return vo;
    }

}
